<?php
/**
 * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

if( PUtil::checkUserLogin() !== true ) {
   exit;
}

OCP\JSON::checkAppEnabled('calendar');

$id = JRequest::getInt('id', 0);
$resultid = JRequest::getInt('resultid', 0);
$object_model = new PCalendarModelObject();
$tpl_model = new PCalendarModelTpl();
$app_model = new PCalendarModelApp();

$data = $app_model->getEventObject($id, false, false);

if(!$data)
   exit('No events found.');

$extra_data = $object_model->getEventExtraData($id);
$extra_data->validemail = JRequest::getInt("validemail", 0);
$object = OC_VObject::parse($data['calendardata']);
$vevent = $object->VEVENT;
$object = $object_model->cleanByAccessClass($id, $object);

$dtstart = $vevent->DTSTART;
$dtend = $object_model->getDTEndFromVEvent($vevent);
$summary = $vevent->getAsString('SUMMARY');

$calendar = new PCalendarModellistview();
$interview_list = $calendar->getAttachmentList('interview');
$interviewers = array();
$tpl_list = array();
$last_modified = null;

$start = JFactory::getDate()->toUnix();
$start = $start - ($start%(30*60)) + 30*60;

$startdate = JHtml::_("date", $start, 'Y-m-d');
$starttime = JHtml::_("date", $start, 'H:i');

if($resultid != PCalendarHelper::INTERVIEW_RESULT_CANCEL && $resultid != PCalendarHelper::INTERVIEW_RESULT_ABSENT){
   switch($dtstart->getDateType()) {
      case Sabre\VObject\Property\DateTime::UTC:
         $config = PFactory::getConfig();
         $timezone = $config->get('offset', 'Asia/Shanghai');
         $timezone = new DateTimeZone($timezone);
         $newDT    = $dtstart->getDateTime();
         $newDT->setTimezone($timezone);
         $dtstart->setDateTime($newDT);
         $newDT    = $dtend->getDateTime();
         $newDT->setTimezone($timezone);
         $dtend->setDateTime($newDT);
      case Sabre\VObject\Property\DateTime::LOCALTZ:
      case Sabre\VObject\Property\DateTime::LOCAL:
         $startdate = $dtstart->getDateTime()->format('Y-m-d');
         $starttime = $dtstart->getDateTime()->format('H:i');
         $allday = false;
         break;
      case Sabre\VObject\Property\DateTime::DATE:
         $startdate = $dtstart->getDateTime()->format('Y-m-d');
         $starttime = '';
         $dtend->getDateTime()->modify('-1 day');
         $allday = true;
         break;
   }
   $interviewers = $object_model->getEventInterviewers($id);
   // Get notify tpl list
   $tpl_list = $tpl_model->getAllTpl('notify');
}
$last_modified = $vevent->__get('LAST-MODIFIED');

if ($last_modified) {
   $lastmodified = $last_modified->getDateTime()->format('U');
}else{
   $lastmodified = 0;
}

// Remove repeat
$calendar_model = new PCalendarModelCalendar();
$calendar_options = $calendar_model->allCalendars(OC_USER::getUser());

$extra_data->type = JRequest::getInt('type', 0);

$tmpl = new OCP\Template('calendar', 'part.editevent');
$tmpl->assign('eventid', $id);
$tmpl->assign('resultid', $resultid);
$tmpl->assign('objecttype', $data['objecttype']);
$tmpl->assign('lastmodified', $lastmodified);
$tmpl->assign('calendar_options', $calendar_options);
$tmpl->assign('modal_title', $summary);
$tmpl->assign('calendar', $data['calendarid']);
$tmpl->assign('startdate', $startdate);
$tmpl->assign('starttime', $starttime);

$tmpl->assign('ex_data', $extra_data);
$tmpl->assign('interviewers', $interviewers);
$tmpl->assign('tpl_list', $tpl_list);
$tmpl->assign('interview_list', $interview_list);
$tmpl->assign('totalfeedbacks', $extra_data->last_round_totalfeedbacks);
// Remove repeat

$tmpl->printpage();